package com.dkd.manage.listener;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.common.config.TopicConfig;
import com.dkd.common.constant.DkdContants;
import com.dkd.manage.domain.Emp;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.domain.dto.TaskDto;
import com.dkd.manage.domain.dto.VendResult;
import com.dkd.manage.service.IEmpService;
import com.dkd.manage.service.ITaskService;
import com.dkd.manage.service.IVendingMachineService;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Objects;

@Component
@Slf4j
public class VendOutListener {

    @Autowired
    private IVendingMachineService vendingMachineService;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private IEmpService empService;
    @KafkaListener(topics = TopicConfig.DKD_SERVER_TOPIC,groupId = "group1")
    public void listen(@RequestBody String message) {
        log.info("接收到出货结果,{}", message);
        //获取售货机编号
//        String InnerCode = StringUtils.split(vendResult.getMessage(), ":")[1].trim();
//        //根据售货机编号查询售货机信息
//        VendingMachine vendingMachine = vendingMachineService.selectVendingMachineByInnerCode(InnerCode);
//        vendingMachine.setRunningStatus(DkdContants.TASK_STATUS_EXCEPTION);
//        vendingMachineService.updateVendingMachine(vendingMachine);
//        log.info("售货机编号{}修改运行状态成功", InnerCode);
//        //根据售货机区域查询附近维修人员列表
//        List<Emp> empList = empService.operationList(InnerCode);
//        if(CollectionUtils.isEmpty(empList)){
//            log.info("售货机编号{}附近没有维修人员", InnerCode);
//            throw new RuntimeException("售货机编号" + InnerCode + "附近没有维修人员");
//        }
//        //设置运维工单，交给运维人员去修理售货机
//        TaskDto taskDto = new TaskDto();
//        taskDto.setInnerCode(InnerCode);
//        taskDto.setCreateType(0L); //创建类型：系统自动创建
//        taskDto.setUserId(empList.get(0).getId()); //指定执行人
//        taskDto.setAssignorId(1L); //指定指派人
//        taskDto.setProductTypeId(DkdContants.TASK_TYPE_REPAIR); //工单类型：设备维修
//        taskDto.setDesc("售货机编号" + InnerCode + "在支付时出现故障，请及时处理");
//        taskService.insertTask(taskDto);
//        log.info("售货机编号{}创建运维工单成功", InnerCode);
    }
}
